package com.bigger.controller;

import com.bigger.dto.LoginDTO;
import com.bigger.dto.UpdatePasswordDTO;
import com.bigger.entity.Employee;
import com.bigger.properties.JwtProperties;
import com.bigger.result.Result;
import com.bigger.service.AdminService;
import com.bigger.utils.JwtUtil;
import com.bigger.vo.LoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private JwtProperties jwtProperties;
    @PostMapping("/login")
    public Result<LoginVO> login(@RequestBody LoginDTO loginDTO){
        Employee employee = adminService.login(loginDTO);
        Map<String,Object> claim=new HashMap<>();
        claim.put("id",employee.getId());
        String token = JwtUtil.createToken(jwtProperties.getAdminKey(), jwtProperties.getAdminTtl(), claim);
        LoginVO loginVO = new LoginVO(employee.getId(), employee.getName(), employee.getUsername(), token);
        return Result.success(loginVO);
    }
    @PutMapping("/update")
    public Result<String> updatePassword(@RequestBody UpdatePasswordDTO updatePasswordDTO){
        adminService.updatePassword(updatePasswordDTO);
        return Result.success("修改密码成功");
    }
    @GetMapping("/get")
    public Result<Employee> getById(){
        Employee employee = adminService.getById();
        employee.setPassword("************");
        return Result.success(employee);
    }
}
